﻿Public Class FMWaliKelas

    'member variables
    Private _cWaliKelas As CWaliKelas
    Private _dView As DataView
    Private _cSemester As CSemester
    Private _semesterAktif As DataTable

    'forms
    Private WithEvents _flSemester As FLSemester
    Private WithEvents _flGuru As FLGuru

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.Text = My.Settings.NamaAplikasi & " [Perwalian Kelas]"
        _cWaliKelas = New CWaliKelas
        _cSemester = New CSemester
    End Sub

#Region "SettinganAwal"
    '-----------------------------------------------------------------------------------------------
    Private IsFormBeingDragged As Boolean = False
    Private MouseDownX As Integer
    Private MouseDownY As Integer

    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PanelJudul.MouseDown, MyBase.MouseDown

        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = True
            MouseDownX = e.X
            MouseDownY = e.Y
        End If
    End Sub

    Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseUp, LabelJudul.MouseUp, PanelJudul.MouseUp

        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = False
        End If
    End Sub

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseMove, LabelJudul.MouseMove, PanelJudul.MouseMove

        If IsFormBeingDragged Then
            Dim temp As Point = New Point()

            temp.X = Me.Location.X + (e.X - MouseDownX)
            temp.Y = Me.Location.Y + (e.Y - MouseDownY)
            Me.Location = temp
            temp = Nothing
        End If
    End Sub


    Private Sub Panel1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles PanelJudul.Paint
        Dim p As New Pen(Color.FromArgb(213, 251, 185), 2)
        'set text box border
        PaintTextBox(Me, e.Graphics, p)

        p.Dispose()
    End Sub

    Private Sub PaintTextBox(cont As Control, g As Graphics, p As Pen)
        For Each ctl As Control In cont.Controls
            If TypeOf ctl Is TextBox Then
                g.DrawRectangle(p, New Rectangle(ctl.Location + New Size(1, 1), ctl.Size - New Size(2, 2)))
            ElseIf TypeOf ctl Is Panel Then
                PaintTextBox(ctl, g, p)
            ElseIf TypeOf ctl Is GroupBox Then
                PaintTextBox(ctl, g, p)
            End If
        Next
    End Sub

    Private Sub SetRegionTextBox(cont As Control)
        For Each ctl As Control In cont.Controls
            If TypeOf ctl Is TextBox Then
                If ctl.Region Is Nothing Then
                    ctl.Region = New Region(New Rectangle(2, 2, ctl.Width - 4, ctl.Height - 4))
                Else
                    ctl.Region = Nothing
                End If
            ElseIf TypeOf ctl Is Panel Then
                SetRegionTextBox(ctl)
            ElseIf TypeOf ctl Is GroupBox Then
                SetRegionTextBox(ctl)
            End If
        Next
    End Sub

    Private Sub btnMaximize_Click(sender As System.Object, e As System.EventArgs) Handles btnMaximize.Click

        WindowState = FormWindowState.Maximized
        btnMinimize.Visible = False
        btnMaximize.Visible = False
        btnClose.Visible = False
    End Sub

    Private Sub btnMinimize_Click(sender As System.Object, e As System.EventArgs) Handles btnMinimize.Click
        WindowState = FormWindowState.Minimized
    End Sub

    Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub FSizeChanged(sender As Object, e As System.EventArgs) Handles Me.SizeChanged
        If WindowState <> FormWindowState.Maximized Then
            btnMinimize.Visible = True
            btnMaximize.Visible = True
            btnClose.Visible = True
        End If
    End Sub
    '-----------------------------------------------------------------------------------------------
#End Region

    Private Sub TampilkanData()
        _dView = _cWaliKelas.getByIdSemester(_semesterAktif(0).Item("id")).DefaultView
        DGMaster.DataSource = _dView
    End Sub

    Private Sub FMWaliKelas_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'get semester aktif
        _semesterAktif = _cSemester.getSemesterAktif()

        'tampilkan semester aktif ke control
        TBSemester.Text = _semesterAktif(0).Item("nama")

        'tampilkan data
        TampilkanData()
    End Sub

    Private Sub DGMaster_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGMaster.CellContentClick
        'jika yg di klik adalah button maka menampilkan daftar form pilih guru
        'menampilkan form pilih guru
        If e.ColumnIndex = CPilih.Index Then
            'mengumpulkan id guru yang telah menjadi wali kelas
            Dim _exceptArray As New List(Of Integer)

            For Each drow As DataGridViewRow In DGMaster.Rows
                If IsDBNull(drow.Cells("CIdGuru").Value) Then

                Else
                    _exceptArray.Add(drow.Cells("CIdGuru").Value)
                End If
            Next

            If _exceptArray.Count > 0 Then
                _flGuru = New FLGuru(_exceptArray)
            Else
                _flGuru = New FLGuru()
            End If

            'form cover
            Dim fcov As New FCover
            fcov.Show()

            _flGuru.ShowDialog()

            fcov.Close()
        ElseIf e.ColumnIndex = CHapus.Index Then
            'hapus guru
            DGMaster.CurrentRow.Cells("CIdGuru").Value = DBNull.Value
            DGMaster.CurrentRow.Cells("CNamaGuru").Value = ""
        End If
    End Sub

    Private Sub DGMaster_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DGMaster.RowPostPaint
        'menampilkan nomor urut pada datagridview
        DGMaster.Rows(e.RowIndex).Cells("CNo").Value = e.RowIndex + 1

        'menampilkan tulisan pada DataGridViewButton
        DGMaster.Rows(e.RowIndex).Cells("CPilih").Value = "Pilih"
        DGMaster.Rows(e.RowIndex).Cells("CHapus").Value = "Hapus"
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        '_flSemester = New FLSemester
        '_flSemester.ShowDialog()
    End Sub

    Private Sub _flSemester_DataTerpilih() Handles _flSemester.DataTerpilih
        'ganti semester terpilih
        _semesterAktif(0).Item("id") = _flSemester.SelectedIDSemester
        _semesterAktif(0).Item("thn") = _flSemester.SelectedTahunSemester
        _semesterAktif(0).Item("nama") = _flsemester.SelectedNamaSemester 
        _semesterAktif(0).Item("tipe") = _flSemester.SelectedTipeSemester
        _semesterAktif(0).Item("aktif") = _flSemester.SelectedAktifSemester

        'tampilkan ke control
        TBSemester.Text = _semesterAktif(0).Item("nama")

        'tampilkan data perwalian kelas ke dalam datagridview
        TampilkanData()
    End Sub

    Private Sub _flGuru_DataTerpilih() Handles _flGuru.DataTerpilih
        'set guru terpilih
        DGMaster.CurrentRow.Cells("CIdGuru").Value = _flGuru.SelectedIDGuru
        DGMaster.CurrentRow.Cells("CNamaGuru").Value = _flGuru.SelectedNamaGuru
    End Sub

    Private Sub SimpanToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpanToolStripMenuItem.Click
        'simpan data perwalian
        '_cWaliKelas.Rubah(_semesterAktif(0).Item("id"), CType(DGMaster.DataSource, DataView).Table)
        Try
            _cWaliKelas.RubahTSql(_semesterAktif(0).Item("id"), CType(DGMaster.DataSource, DataView).Table)
            MessageBox.Show("Data tersimpan", "Informasi", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

    Private Sub KeluarToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KeluarToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub Button5_Click(sender As System.Object, e As System.EventArgs) Handles Button5.Click
        SimpanToolStripMenuItem.PerformClick()
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        KeluarToolStripMenuItem.PerformClick()
    End Sub


    Private Sub TBSemester_Enter(sender As Object, e As System.EventArgs) Handles TBSemester.Enter, TBSemester.Click
        'form cover
        Dim fcov As New FCover
        fcov.Show()

        _flSemester = New FLSemester
        _flSemester.ShowDialog()

        fcov.Close()
    End Sub

End Class